using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Core
{
    public class HelpItem : IEquatable<HelpItem>
    {
        private string _id = string.Empty;

        public string id
        {
            get { return _id; }
            set { _id = value; }
        }

        private HelpItemManifestEntry _manifestEntry = new HelpItemManifestEntry();

        [XmlIgnore]
        public HelpItemManifestEntry ManifestEntry
        {
            get { return _manifestEntry; }
            set { _manifestEntry = value; }
        }

        public void SaveManifestEntry()
        {
            if (HCProjectManager.CurrentProject != null)
            {
                string fileName = string.Concat(HCProjectManager.CurrentProject.MetaDataFolder.FullName, "\\", this.id, ".xml");

                using (StreamWriter sw = new StreamWriter(fileName, false))
                {
                    string xml = Utils.Serialize(this, this.GetType());
                    sw.WriteLine(xml);
                }

                //Save this entry to the manifest
                if (ManifestEntry != null)
                {
                    HCManifest manifest = HCProjectManager.CurrentProject.ProjectManifest;
                    HelpItemManifestEntry helpItemEntry = manifest.helpItemEntries.Find(delegate(HelpItemManifestEntry hi) { return string.Compare(hi.id, this.id, true) == 0; });
                    if (helpItemEntry == null)
                    {
                        //not found, add it
                        manifest.helpItemEntries.Add(ManifestEntry);
                    }
                    else
                    {
                        //found, modify it
                        helpItemEntry = ManifestEntry;
                    }
                }
            }
        }

        /// <summary>
        /// delete this help item, this involves
        ///     .delete the manifest entry
        ///     .delete the meta data file
        ///     .if this help item contains actual files (help topic, help image...), the file is to be deleted also, but this logic is to be impleted on the helpImage, helpTopic level 
        ///     with override to this method
        /// </summary>
        public virtual void Delete()
        {
            if (HCProjectManager.CurrentProject != null)
            {
                //delete the manifest entry
                if (ManifestEntry != null)
                {
                    HCManifest manifest = HCProjectManager.CurrentProject.ProjectManifest;
                    HelpItemManifestEntry helpItemEntry = manifest.helpItemEntries.Find(delegate(HelpItemManifestEntry hi) { return string.Compare(hi.id, this.id, true) == 0; });
                    //remove it
                    manifest.helpItemEntries.Remove(helpItemEntry);
                    manifest.SaveToDisk();
                }

                //delete the meta data file
                string fileName = string.Concat(HCProjectManager.CurrentProject.MetaDataFolder.FullName, "\\", this.id, ".xml");

                if (File.Exists(fileName))
                    File.Delete(fileName);

                //delete from this project
                HelpCategory cat = HCProjectManager.CurrentProject.HelpCategory.Find(delegate(HelpCategory current) { return string.Compare(current.id, this.id, true) == 0; });
                if (cat != null) HCProjectManager.CurrentProject.HelpCategory.Remove(cat);

                HelpContext con = HCProjectManager.CurrentProject.HelpContext.Find(delegate(HelpContext current) { return string.Compare(current.id, this.id, true) == 0; });
                if (con != null) HCProjectManager.CurrentProject.HelpContext.Remove(con);

                HelpCss css = HCProjectManager.CurrentProject.HelpCss.Find(delegate(HelpCss current) { return string.Compare(current.id, this.id, true) == 0; });
                if (css != null) HCProjectManager.CurrentProject.HelpCss.Remove(css);

                HelpImage img = HCProjectManager.CurrentProject.HelpImage.Find(delegate(HelpImage current) { return string.Compare(current.id, this.id, true) == 0; });
                if (img != null) HCProjectManager.CurrentProject.HelpImage.Remove(img);

                HelpScript script = HCProjectManager.CurrentProject.HelpScript.Find(delegate(HelpScript current) { return string.Compare(current.id, this.id, true) == 0; });
                if (script != null) HCProjectManager.CurrentProject.HelpScript.Remove(script);

                HelpTopic topic = HCProjectManager.CurrentProject.HelpTopic.Find(delegate(HelpTopic current) { return string.Compare(current.id, this.id, true) == 0; });
                if (topic != null) HCProjectManager.CurrentProject.HelpTopic.Remove(topic);
            }
        }

        /// <summary>
        /// IEquable
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool Equals(HelpItem other)
        {
            return string.Compare(this.id, other.id, true) == 0;
        }


    }
}
